import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sun.awt.image.ImageFormatException;

public class CollectActivity extends Thread {
	   private int[] clickXY;
	   private int current;
	   private int initial;
	   private int status;
	   private PennerInfo info;
	   private PennerStats stats;
	   private Connection connection;
	   private AntiCaptcha antiCaptcha;
	   private String sCaptchaImg = "";
	   private String sConcentration ="";
	   private String sStatus = "Bereit";
	   private javax.swing.Timer activityTimer=null;
	   private long lastStatusTime;
	   private String sCollectTime="10";
	   
	   private int time2Status=53000;

	   private int READY = 0;
	   private int COLLECTING = 1;
//	   private int CONCENTRATING = 2;
	   private int FIGHTING = 3;
	   private int NOTFINISHED = 4;
	   private int WAIT = 5;
	   private int START_COLLECT = 6;
	   private int LOGEDIN = 94;
	   private int NO_LOGIN = 95;
	   private int LOGEDOUT = 96;
	   private int NOTCRACKED = 97;
	   private int CAP_ERROR = 98;
	   private int ERROR = 99;
	   private int LOG_ERROR = 100;
	   
	   private int initialBottles = 0;
	   private ResourceBundle resbundle;

	   
	   public CollectActivity(int t,Connection pennerConn, PennerInfo pennerInfo, PennerStats pennerStats, ResourceBundle res) throws IOException {
		   current = 0;
		   initial = t;
		   connection = pennerConn;
		   info = pennerInfo;
		   stats = pennerStats;
		   antiCaptcha = new AntiCaptcha();
		   status = 0;
		   initialBottles = info.craps;
		   resbundle = res;
	   }

	   public int getInitial() {
		   return initial;
	   }

	   public int getCurrent() {
		   return current;
	   }
	   
	   public String getCaptcha() {
		   return sCaptchaImg;
	   }
	   
	   public String getConcentration() {
		   return sConcentration;
	   }
	   
	   public String getCollectTime() {
		   return sCollectTime;
	   }
	   
	   public String getStatusLine() {
		   return sStatus;
	   }
		   
	   public int getInitialBottles() {
		   return initialBottles;
	   }

	   public int getStatus() {
		   int tStatus = READY;
		   String sPage = "";
		   sCollectTime = "10";
		   
		   try {
//			   while(connection.gettingPage) {
				   //do nothing
//			   }
			   sPage = connection.getPage(connection.sSideURL+connection.sActivityURL);
		   } catch (IOException e) {
			   return ERROR;
		   }
		   
		   Pattern p = Pattern.compile("<li class=\"icon rank\">[ \t\r\n]*<a href=\"/activities/\"[ \t\r\n].*>[ \t\r\n]*<script type=\"text/javascript\">counter.(.*).</script>");
		   Matcher matcher = p.matcher(sPage);
		   if (matcher.find()) {
			   current = Integer.valueOf(sPage.substring(matcher.start(1), matcher.end(1)));
			   if (current < 0) {
				   tStatus = NOTFINISHED;
			   }
		   }
		   
		   p = Pattern.compile("<input type=\"hidden\" name=\"konzentrieren\" value=\"(.*?)\">");
		   matcher = p.matcher(sPage);
		   if (matcher.find()) {
			   sConcentration = sPage.substring(matcher.start(1), matcher.end(1));
		   }
		   		   
		   p = Pattern.compile("document.getElementById[(]\"captcha_img\"[)].src=\"(.*?)\";");
		   matcher = p.matcher(sPage);
		   if (matcher.find()) {
			   sCaptchaImg = sPage.substring(matcher.start(1), matcher.end(1));
		   }
		   		   
		   p = Pattern.compile("<strong>Pfandflaschensammeln</strong>");
		   matcher = p.matcher(sPage);
		   if (!matcher.find()) {
			   p = Pattern.compile("Du konzentrierst dich gerade!");
			   matcher = p.matcher(sPage);
			   if (matcher.find()) {
				   tStatus = READY;
				   sCollectTime="60";
			   }
		   }
		   
		   p = Pattern.compile("gerade einen Angriff durch");
		   matcher = p.matcher(sPage);
		   if (matcher.find()) {
			   tStatus = FIGHTING;
		   }
		   
		   p = Pattern.compile("Du bist auf Pfandflaschensuche:");
		   matcher = p.matcher(sPage);
		   if (matcher.find()) {
			   tStatus = COLLECTING;
		   }
		   		   
		   p = Pattern.compile("<form id=\"loginform\"");
		   matcher = p.matcher(sPage);
		   if (matcher.find()) {
			   tStatus = LOGEDOUT;
		   }
		   
		   return tStatus;
	   }

	   public void run() {
	      while (!interrupted()) {
	         if (current <= 0 && (System.currentTimeMillis() - lastStatusTime) > time2Status) {
				 status = getStatus();
				 lastStatusTime = System.currentTimeMillis();
			 }
	         if (!connection.LogedIn) {
	        	 status = NO_LOGIN;
	        	 stats.setStatus(resbundle.getString("not_logedin"));
	         } else {
	        	 if (status==NO_LOGIN) {
	        		 status = LOGEDIN;
		        	 stats.setStatus(resbundle.getString("logedin"));
	        	 }
	         }
	         switch (status) {
			 case 0:
				 sStatus = "Bereit";
				 time2Status = 53000;
				 try {
						clickXY = antiCaptcha.GetNumberPosition(connection, sCaptchaImg);
						connection.startCollecting(MultiBot.collectActivity, clickXY);
						if (connection.collectStarted) {
							stats.goodCaptcha();
							status = START_COLLECT;
						} else {
							stats.badCaptcha();
							status = NOTCRACKED;
						}
					} catch (IOException e) {
						status = CAP_ERROR;
					} catch (ImageFormatException e) {
						status = CAP_ERROR;
					} catch (Exception e) {
						status = CAP_ERROR;
					}
				 break;
			 case 1:
				 sStatus = "Sammeln";
				 time2Status = 53000;
				 break;
			 case 2:
				 sStatus = "Du konzentrierst Dich";
				 time2Status = 242300;
				 break;
			 case 3:
				 sStatus = "Kampf";
				 time2Status = info.attTime*1000;
			 case 4:
				 sStatus = "Am Beenden";
				 time2Status = 27000;
				 break;
			 case 5:
				 sStatus = "Warten...";
				 time2Status = 34000;
				 break;
			 case 6:
				 sStatus = "Sammeln gestartet";
				 time2Status = 10000;
				 break;
			 case 94:
				 sStatus = "Angemeldet";
				 time2Status = 10000;
				 break;
			 case 95:
				 sStatus = "Nicht angemeldet!";
				 time2Status = 34000;
				 break;
			 case 96:
				 sStatus = "Relogin";
				 stats.setStatus(resbundle.getString("relogin"));
				 try {
						if (connection.sPWD!="" & connection.sUID!="") {
							connection.relogin();
							stats.setStatus(resbundle.getString("logedin"));
							stats.reloginCount++;
						} else {
							status = NO_LOGIN;
							stats.setStatus(resbundle.getString("not_logedin"));
						}
					} catch (Exception e) {
						status = LOG_ERROR;
					}
				 time2Status = 10000;
				 break;
			 case 97: 
				 sStatus = "Zahl nicht gefunden!";
				 time2Status = 24000;
				 break;
			 case 98: 
				 sStatus = "Captcha Fehler!";
				 time2Status = 24000;
				 break;
			 case 99: 
				 sStatus = "Fehler!";
				 time2Status = 53000;
				 break;
			 case 100: 
				 sStatus = "Login-Fehler!";
				 stats.setStatus(resbundle.getString("login_error"));
				 time2Status = 53000;
				 break;
			 }
//			 System.out.println("time2Status: "+time2Status);
			 /* Timer zum Ende des Flaschensammeln setzen */
			 if (current > 0) {
				 if (activityTimer==null) {
					 activityTimer = new javax.swing.Timer(1000,
						 new ActionListener()
			                {  public void actionPerformed(ActionEvent event)
			                 	{  
			                		current--;
			                		if (current < 0) {
			                			status = WAIT;
			                			activityTimer.stop();
			                		}
			                    }
			    	         });
				 } else if (!activityTimer.isRunning()) {
					 activityTimer.start();
				 }
			 }
			 try {
				CollectActivity.sleep(1000);
			} catch (InterruptedException e) {
	        	stats.setStatus("Sammelschlaf Fehler");
			}
	      }
	   }


}
